﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.Admin.Filters;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class RolesController : Controller
    {
        //反转注入
        private IServicePermissions _permission;
        private IServiceRoles _role;
        public RolesController(IServicePermissions permission, IServiceRoles roles)
        {
            _permission = permission;   
            _role = roles;  
        }
        [CheckMyPermission("lookRole")]  //权限过滤
        /// <summary>
        /// 角色列表(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 角色列表显示(方法)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult RolesList(int start, int length, string name)
        {
            int count = 0;
            var list = _role.GetList(start, length, name, ref count);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsFiltered = count;
            pageModel.recordsTotal = count;
            return Json(pageModel);
        }
        /// <summary>
        /// 添加角色(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            var perList = _permission.GetList();
            ViewBag.perList = perList;  
            return View();
        }
        /// <summary>
        /// 实现添加角色(方法)
        /// </summary>
        /// <param name="rolesEditDTO"></param>
        /// <returns></returns>
        public ActionResult AddRoles(RolesEditDTO rolesEditDTO)
        {
            var result = _role.Add(rolesEditDTO);             
            return Json(result);
        }
        /// <summary>
        /// 根据id查询出该条数据并传递给视图(方法+视图)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            RolesEditDTO dto = _role.Get(id);
            var perList = _permission.GetList();
            ViewBag.perList = perList;
            return View(dto);   
        }
        /// <summary>
        /// 实现修改角色(方法)
        /// </summary>
        /// <param name="rolesEdit"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditRole(RolesEditDTO rolesEdit)
        { 
            var res=_role.Update(rolesEdit);    
            return Json(res);   
        }
        /// <summary>
        /// 删除角色(方法)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(long id)
        { 
            var result=_role.Delete(id);    
            return Json(result);    
        }
        /// <summary>
        /// 批量删除角色(方法)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        { 
           var result=_role.DeleteBatch(ids);   
            return Json(result);    
        }
    }
}